package com.cgtong.android.mvp.presenter;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.cgtong.android.App;
import com.cgtong.android.bean.BannerBean;
import com.cgtong.android.bean.UserInfoBean;
import com.cgtong.android.db.UserAccount;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.mvp.contract.SplashContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;
import com.zhouyou.http.model.HttpParams;

import java.util.Date;
import java.util.List;

/**
 * Created by Sky.Bing on 2017/10/27.
 */

public class SplashPresenter implements SplashContract.Presenter {
    private SplashContract.View mView;

    public SplashPresenter(SplashContract.View view) {
        this.mView = view;
    }

    @Override
    public void getUserByToken(final String token) {
        SPUtils.getInstance().put(ConstantManager.FIRST_TOKEN_VALIDATE, true);
        HttpParams params = new HttpParams();
        params.put("accessToken", token);
        params.put("platform", ConstantManager.PLATFORM);
        EasyHttp.getInstance().addCommonParams(params);
        EasyHttp.post(ApiManager.URL_GETUSERBYTOKEN)
                .execute(new SimpleCallBack<UserInfoBean.UserInfoBeanObj>() {
                    @Override
                    public void onError(ApiException e) {
                        mView.onLoginError(e.getCode()); //get usertoken error
                    }

                    @Override
                    public void onSuccess(UserInfoBean.UserInfoBeanObj userInfoBeanObj) {
                        UserAccount userAccount = new UserAccount(userInfoBeanObj);
                        userAccount.setLoginStatus(ConstantManager.LOGIN_IN);
                        userAccount.setLoginTime(TimeUtils.date2String(new Date()));
                        UserAccount userFromDb = App.getInstance().findAccountByUserId(String.valueOf(userAccount.getUserId()));
                        if (null != userFromDb) { //exists
                            //将redEnvelopes设置成默认值，litepal框架设计如此
                            if(userAccount.getRedEnvelopes() == 0){
                                userAccount.setToDefault("redEnvelopes");
                            }
                            userAccount.setId(userFromDb.getId());
                            userAccount.update(userFromDb.getId());
                            mView.onLoginSuccess(); //get usertoken success
                        } else {
                            if (userAccount.save()) {
                                mView.onLoginSuccess(); //get usertoken success
                            } else {
                                mView.onLoginError(0);  //get usertoken error
                            }
                        }
                    }
                });
    }

    @Override
    public void getBannerList(SimpleCallBack callBack) {
        EasyHttp.post(ApiManager.URL_GET_BANNER_LIST)
                .params("columnId", String.valueOf(ConstantManager.BANNER_COLUMN_NEWS))
                .execute(new CallBackProxy<ApiResult<List<BannerBean>>, List<BannerBean>>(callBack) {
                });
    }
}